package tech.mobi.child.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import tech.mobi.child.CZDAClientListTxtViewMainAct;
import tech.mobi.child.R;
import tech.mobi.child.bean.GroupBean;
import tech.mobi.child.utils.AsyncImageLoader;
import tech.mobi.child.utils.AsyncImageLoader.ImageCallback;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GoodClientListAdapter<T> extends CustomAdapter {
	private final static String TAG = "TuanCanListAdapter";
	private ListView listView;
	private AsyncImageLoader asyncImageLoader;
	private Context context;
	private List<GroupBean> data;
	private LayoutInflater inflater;
	
	private String strTypeId;
	private String strTitleName;

	@SuppressWarnings("unchecked")
	public GoodClientListAdapter(ArrayList<T> data, Context context) {
		super(data, context);
	}

	@SuppressWarnings("unchecked")
	public GoodClientListAdapter(ArrayList<T> data, ListView listView,
			Context context,String typeId,String titleName) {
		super(data, context);

		this.listView = listView;
		asyncImageLoader = new AsyncImageLoader();
		this.context = context;
		this.data = (List<GroupBean>) data;
		inflater = ((Activity) context).getLayoutInflater();
		this.strTypeId = typeId;
		this.strTitleName = titleName;
	}

	@SuppressWarnings("unchecked")
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) context;
		Log.i(TAG, "position----" + position);
		// Inflate the views from XML
		View rowView = convertView;
		ViewHolder viewCache;
		if (rowView == null) {
			rowView = inflater.inflate(R.layout.good_client_list_items, null);
			viewCache = new ViewHolder();
			viewCache.image = (ImageView) rowView.findViewById(R.id.image);
			viewCache.title = (TextView) rowView.findViewById(R.id.title);
			viewCache.info = (TextView) rowView.findViewById(R.id.info);
			viewCache.price = (TextView) rowView.findViewById(R.id.price);
			viewCache.price2 = (TextView) rowView.findViewById(R.id.price2);
			viewCache.people = (TextView) rowView.findViewById(R.id.people);
			// viewCache.releaseID = (TextView)
			// rowView.findViewById(R.id.releaseID);
			rowView.setTag(viewCache);
		} else {
			viewCache = (ViewHolder) rowView.getTag();
		}
		GroupBean mealInfo = (GroupBean) getItem(position);

		// Load the image and set it on the ImageView

//		String imageUrl = mealInfo.getImgUrl();
		String imageUrl = mealInfo.getImagezoom();
		ImageView imageView = viewCache.image;
		imageView.setTag(imageUrl);

		String path = asyncImageLoader.loadDrawable(imageUrl,
				activity.getCacheDir(), new ImageCallback() {
					public void imageLoaded(String imagePath, String imageUrl) {
						ImageView imageViewByTag = (ImageView) listView
								.findViewWithTag(imageUrl);
						if (imageViewByTag != null && imagePath != null
								&& !"".equals(imagePath)) {
//							Bitmap bitmap = FileUtil.getBitmapOfCompress(
//									imagePath, 200 * 200);
							File file = new File(imagePath);
							Bitmap bitmap = null;
							if(file.exists()){
								//BitmapFactory.Options opt = new BitmapFactory.Options();
								//opt.inJustDecodeBounds = true;
								bitmap = BitmapFactory.decodeFile(file.getPath());
							}

							if (bitmap != null)
								imageViewByTag.setImageBitmap(bitmap);
						}
					}
				});
		if (path == null || "".equals(path)) {
			imageView.setImageResource(R.drawable.desklogo);
		} else {
//			Bitmap bitmap = FileUtil.getBitmapOfCompress(path, 200 * 200);
			Bitmap bitmap = BitmapFactory.decodeFile(path);
			if (bitmap != null) {
				imageView.setImageBitmap(bitmap);
			} else {
				imageView.setImageResource(R.drawable.desklogo);
			}
		}
		// Set the text on the TextView
		viewCache.title.setText(mealInfo.getTitle());
		viewCache.info.setText(mealInfo.getProduct_name()); //
		viewCache.price.setText(mealInfo.getPrice() + "元");
		viewCache.price2.setText(mealInfo.getMarketPrice() + "元 ");
		viewCache.people.setText(mealInfo.getXs_data() + "人");
		// viewCache.button.setOnClickListener((GroupListNewAct)context);
		// viewCache.button.setTag(mealInfo);
		// viewCache.releaseID.setText(mealInfo.getReleaseId());
		viewCache.price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		viewCache.price2.setTag(mealInfo);
		rowView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				GroupBean groupBean = (GroupBean) ((TextView) v
						.findViewById(R.id.price2)).getTag();
//				Session session = Session.getSession();
//				if (session.get("goodsData") != null) {
//					session.cleanUpSession();
//					session.remove("goodsData");
//				}
//				session.put("goodsData", groupBean);
				
				Intent intent = new Intent(context, CZDAClientListTxtViewMainAct.class);
				intent.putExtra("titleName", strTitleName);
				intent.putExtra("typeId", strTypeId);
				intent.putExtra("sId", groupBean.getReleaseId());
				context.startActivity(intent);
				
				
				Toast.makeText(context, "点了：" + groupBean.getReleaseId(), Toast.LENGTH_LONG).show();

//				Intent intent = new Intent(context, GoodClientDetailAct.class);
//				intent.putExtra("title", R.string.good_client_detail);
//				intent.putExtra("id", groupBean.getReleaseId());
//				context.startActivity(intent);

			}

		});

		return rowView;
	}

	private class ViewHolder {

		public ImageView image;
		public TextView title;
		public TextView info;
		public TextView price;
		public TextView price2;
		public TextView people;
		// public TextView releaseID;
	}

	public void setData(ArrayList<GroupBean> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}